/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webidewar.jsf;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import org.WebIDE.ejb.entities.GroupsResources;
//import webidewar.controller.GroupsResourcesFacade;
import webidewar.jsf.util.JsfUtil;
//import webidewar.controller.exceptions.NonexistentEntityException;
import org.WebIDE.ejb.entities.GroupsResourcesPK;
import webidewar.jsf.util.PagingInfo;
import org.WebIDE.ejb.sessions.*;
import org.WebIDE.ejb.sessions.exceptions.NonexistentEntityException;
/**
 *
 * @author huy
 */
public class GroupsResourcesController {
    
    private GroupsResourcesFacadeRemote groupsResourcesFacade;

    public GroupsResourcesFacadeRemote getGroupsResourcesFacade() {
        return groupsResourcesFacade;
    }
    @EJB
    public void setGroupsResourcesFacade(GroupsResourcesFacadeRemote groupsResourcesFacade) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getApplication().getELResolver().setValue(facesContext.getELContext(), null, "groupsResourcesJpa", groupsResourcesFacade);

        this.groupsResourcesFacade = groupsResourcesFacade;
    }

    public GroupsResourcesController() {

        pagingInfo = new PagingInfo();
        converter = new GroupsResourcesConverter();
    }
    private GroupsResources groupsResources = null;
    private List<GroupsResources> groupsResourcesItems = null;
    //private GroupsResourcesFacade groupsResourcesFacade = null;
    private GroupsResourcesConverter converter = null;
    private PagingInfo pagingInfo = null;

    public PagingInfo getPagingInfo() {
        if (pagingInfo.getItemCount() == -1) {
            pagingInfo.setItemCount(groupsResourcesFacade.getGroupsResourcesCount());
        }
        return pagingInfo;
    }

    public SelectItem[] getGroupsResourcesItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(groupsResourcesFacade.findGroupsResourcesEntities(), false);
    }

    public SelectItem[] getGroupsResourcesItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(groupsResourcesFacade.findGroupsResourcesEntities(), true);
    }

    public GroupsResources getGroupsResources() {
        if (groupsResources == null) {
            groupsResources = (GroupsResources) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentGroupsResources", converter, null);
        }
        if (groupsResources == null) {
            groupsResources = new GroupsResources();
        }
        return groupsResources;
    }

    public String listSetup() {
        reset(true);
        return "groupsResources_list";
    }

    public String createSetup() {
        reset(false);
        groupsResources = new GroupsResources();
        groupsResources.setGroupsResourcesPK(new GroupsResourcesPK());
        return "groupsResources_create";
    }

    public String create() {
        try {
            groupsResourcesFacade.create(groupsResources);
            JsfUtil.addSuccessMessage("GroupsResources was successfully created.");
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return listSetup();
    }

    public String detailSetup() {
        return scalarSetup("groupsResources_detail");
    }

    public String editSetup() {
        return scalarSetup("groupsResources_edit");
    }

    private String scalarSetup(String destination) {
        reset(false);
        groupsResources = (GroupsResources) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentGroupsResources", converter, null);
        if (groupsResources == null) {
            String requestGroupsResourcesString = JsfUtil.getRequestParameter("jsfcrud.currentGroupsResources");
            JsfUtil.addErrorMessage("The groupsResources with id " + requestGroupsResourcesString + " no longer exists.");
            return relatedOrListOutcome();
        }
        return destination;
    }

    public String edit() {
        groupsResources.getGroupsResourcesPK().setGroupName(groupsResources.getGroups().getGroupName());
        groupsResources.getGroupsResourcesPK().setResourceId(groupsResources.getResources().getId());
        String groupsResourcesString = converter.getAsString(FacesContext.getCurrentInstance(), null, groupsResources);
        String currentGroupsResourcesString = JsfUtil.getRequestParameter("jsfcrud.currentGroupsResources");
        if (groupsResourcesString == null || groupsResourcesString.length() == 0 || !groupsResourcesString.equals(currentGroupsResourcesString)) {
            String outcome = editSetup();
            if ("groupsResources_edit".equals(outcome)) {
                JsfUtil.addErrorMessage("Could not edit groupsResources. Try again.");
            }
            return outcome;
        }
        try {
            groupsResourcesFacade.edit(groupsResources);
            JsfUtil.addSuccessMessage("GroupsResources was successfully updated.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return listSetup();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return detailSetup();
    }

    public String destroy() {
        String idAsString = JsfUtil.getRequestParameter("jsfcrud.currentGroupsResources");
        GroupsResourcesPK id = converter.getId(idAsString);
        try {
            groupsResourcesFacade.destroy(id);
            JsfUtil.addSuccessMessage("GroupsResources was successfully deleted.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return relatedOrListOutcome();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return relatedOrListOutcome();
    }

    private String relatedOrListOutcome() {
        String relatedControllerOutcome = relatedControllerOutcome();
        if (relatedControllerOutcome != null) {
            return relatedControllerOutcome;
        }
        return listSetup();
    }

    public List<GroupsResources> getGroupsResourcesItems() {
        if (groupsResourcesItems == null) {
            getPagingInfo();
            groupsResourcesItems = groupsResourcesFacade.findGroupsResourcesEntities(pagingInfo.getBatchSize(), pagingInfo.getFirstItem());
        }
        return groupsResourcesItems;
    }

    public String next() {
        reset(false);
        getPagingInfo().nextPage();
        return "groupsResources_list";
    }

    public String prev() {
        reset(false);
        getPagingInfo().previousPage();
        return "groupsResources_list";
    }

    private String relatedControllerOutcome() {
        String relatedControllerString = JsfUtil.getRequestParameter("jsfcrud.relatedController");
        String relatedControllerTypeString = JsfUtil.getRequestParameter("jsfcrud.relatedControllerType");
        if (relatedControllerString != null && relatedControllerTypeString != null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
            try {
                Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
                Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
                return (String) detailSetupMethod.invoke(relatedController);
            } catch (ClassNotFoundException e) {
                throw new FacesException(e);
            } catch (NoSuchMethodException e) {
                throw new FacesException(e);
            } catch (IllegalAccessException e) {
                throw new FacesException(e);
            } catch (InvocationTargetException e) {
                throw new FacesException(e);
            }
        }
        return null;
    }

    private void reset(boolean resetFirstItem) {
        groupsResources = null;
        groupsResourcesItems = null;
        pagingInfo.setItemCount(-1);
        if (resetFirstItem) {
            pagingInfo.setFirstItem(0);
        }
    }

    public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
        GroupsResources newGroupsResources = new GroupsResources();
        newGroupsResources.setGroupsResourcesPK(new GroupsResourcesPK());
        String newGroupsResourcesString = converter.getAsString(FacesContext.getCurrentInstance(), null, newGroupsResources);
        String groupsResourcesString = converter.getAsString(FacesContext.getCurrentInstance(), null, groupsResources);
        if (!newGroupsResourcesString.equals(groupsResourcesString)) {
            createSetup();
        }
    }

    public Converter getConverter() {
        return converter;
    }

}
